package com.shjy.driver.widget;

import android.app.Activity;
import android.app.Dialog;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shjy.driver.R;


/**
 * Created by zhouhuiming on 16/11/17.
 */

public class BlueToothConnectMessageDialog extends Dialog implements View.OnClickListener{
    private View view;
    private Button buttonConnect;
    private TextView textViewMessage;
    private int max = 10;

    public BlueToothConnectMessageDialog(Activity context, View.OnClickListener onClickListener, String message) {
        super(context);
        view = LayoutInflater.from(context).inflate(R.layout.dialog_connect_success_bluetooth,null);
        textViewMessage = (TextView) view.findViewById(R.id.textViewMessage);
        if(!TextUtils.isEmpty(message)){
            textViewMessage.setText(message);
        }
        buttonConnect = (Button) view.findViewById(R.id.buttonDialogConfirm);
        buttonConnect.setOnClickListener(onClickListener);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = 286;
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(429,330);
        getWindow().setAttributes(params);
        setContentView(view,layoutParams);
    }

    private void showDelay(){
        buttonConnect.postDelayed(new Runnable() {
            @Override
            public void run() {
                if(max >0){
                    buttonConnect.setText("确定("+(--max)+")");
                    showDelay();
                }else {
                    buttonConnect.callOnClick();
                    dismiss();
                }
            }
        },1000);
    }

    @Override
    public void show() {
        showDelay();
        super.show();
    }

    @Override
    public void onClick(View v) {
        dismiss();
    }
}
